﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TLECDataLayer
{
    public class Categories
    {
        public static void CreateCategory(string cat)
        {
            if (!string.IsNullOrEmpty(cat))
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var res =
                    (from c in context.Categories
                     where c.CategoryType == cat
                     select c).FirstOrDefault();

                if (res == null)
                {
                    Category c = new Category()
                    {
                        CategoryType = cat
                    };

                    context.Categories.InsertOnSubmit(c);
                    context.SubmitChanges();
                }
            }
            else
            {
                throw new ArgumentException("Invalid categorie type");
            }

        }

        public static void UpdateCategory(int catId, string cat)
        {
            if (!string.IsNullOrEmpty(cat))
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var res =
                    (from c in context.Categories
                     where c.CategoryId == catId
                     select c).FirstOrDefault();

                if (res != null)
                {
                    res.CategoryType = cat;
                    context.SubmitChanges();
                }
                else
                {
                    throw new ArgumentException("Invalid category id");
                }
            }
            else
            {
                throw new ArgumentException("Invalid category type");
            }
        }

        public static void DeleteCategory(int catId)
        { 
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from c in context.Categories
                    where c.CategoryId == catId
                    select c).FirstOrDefault();

            if (res != null)
            {
                if (Events.GetAllUpcomingEventsByCategory(catId).Count() == 0)
                {
                    context.Categories.DeleteOnSubmit(res);
                    context.SubmitChanges();
                }
            }
            else
            {
                throw new ArgumentException("Invalid category id");
            }
        }

        public static IQueryable<Category> GetAllCategories()
        {
            TLECDataDataContext context = new TLECDataDataContext();

            return context.Categories;
        }

        public static Category GetCategoryById(int catId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from c in context.Categories
                 where c.CategoryId == catId
                 select c).FirstOrDefault();

            return res;
        }
    }
}
